<?php
namespace App\Util;
use App\Exceptions\WorkException;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

trait VerifyRequest
{


    #regin--------------------   图片验证码   ----------------------------
    /**
     * @param $key
     * @param  null  $default
     * @return mixed
     * @throws WorkException
     */
    public function checkPubTitle($key, $default = null , $code=Code::Wx_CAPTCHA_ILLEGAL)
    {

        return $this->checkData($key, $default,  'required|max:100｜min:4',$code );
    }

    #regin--------------------   基础验证邮箱   ----------------------------
    /**
     * @param $key
     * @param  null  $default
     * @return mixed
     * @throws WorkException
     */
    public function checkEmail($key, $default = null)
    {
        return $this->checkData($key, $default, 'email:rfc,dns');
    }

    #regin--------------------   基础验证昵称标题   ----------------------------
    /**
     * @param $key
     * @param  null  $default
     * @return mixed
     * @throws WorkException
     */
    public function checkTitle($key, $default = null)
    {
        return $this->checkData($key, $default, 'required|max:50｜min:3');
    }

    #regin--------------------   基础验证客户端传递ID   ----------------------------
    /**
     * @param $key
     * @param  null  $default
     * @return mixed
     * @throws WorkException
     */
    public function checkId($key, $default = null)
    {
        return $this->checkData($key, $default, 'integer|digits_between:1,15|min:1');
    }
    #regin--------------------   基础验证客户端传递字符串   ----------------------------
    /**
     * @param $key
     * @param  null  $default
     * @return mixed
     * @throws WorkException
     */
    public function checkString($key, $default = null)
    {
        return $this->checkData($key, $default, 'string');
    }
    #regin--------------------   基础验证客户端传递bool类型   ----------------------------
    /**
     * @param $key
     * @param  null  $default
     * @return mixed
     * @throws WorkException
     */
    public function checkBoolean($key, $default = null)
    {
        return $this->checkData($key, $default, 'boolean');
    }
    #regin--------------------   基础验证客户端传递int类型   ----------------------------
    /**
     * @param $key
     * @param  null  $default
     * @return mixed
     * @throws WorkException
     */
    public function checkInteger($key, $default = null)
    {
        return $this->checkData($key, $default, 'integer');
    }
    #regin--------------------   基础验证客户端传递int类型最小长度为1   ----------------------------
    /**
     * @param $key
     * @param  null  $default
     * @return mixed
     * @throws WorkException
     */
    public function checkPositiveInteger($key, $default = null)
    {
        return $this->checkData($key, $default, 'integer|min:1');
    }

    #regin--------------------   基础验证客户端传递枚举类型   ----------------------------
    /**
     * @param $key
     * @param  null  $default
     * @param  array  $enum
     * @return mixed
     * @throws WorkException
     */
    public function checkEnum($key, $default = null, $enum = [])
    {
        return $this->checkData($key, $default, Rule::in($enum));
    }

    /**
     * @param $key
     * @param  null  $default
     * @return mixed
     * @throws WorkException
     */
    public function checkArrayNotEmpty($key, $default = null)
    {
        return $this->checkData($key, $default, 'array|min:1');
    }
    #regin--------------------   手机号验证  ----------------------------
    /**
     * 手机号验证
     * @param $key
     * @param  null  $default
     * @return mixed
     * @throws BusinessException
     */
    public function checkMobile($key, $default = null)
    {
        return $this->checkData($key, $default, 'regex:/^1[0-9]{10}$/', Code::AUTH_INVALID_MOBILE);
    }
    #regin--------------------   客户端request参数统一处理入口   ----------------------------
    /**
     * @param $key
     * @param $default
     * @param $rule
     * @param  $code
     * @return mixed
     * @throws WorkException
     */
    private function checkData( $key , $default , $rule , $code = Code::CLT_ILLEGAL )
    {
        //获取需要验证的参数并且简单判断
        $value = request()->input( $key , $default );

        if( is_null($key) && is_null($default)) return $value;

        //使用laravel validator 门面根据不同的验证规则进行验证
        $errors = Validator::make([$key => $value],[$key=>$rule]);
        if ( $errors->fails() ){

            throw new WorkException( $code);
        }

        return $value;

    }
}
